#include <bits/stdc++.h>
using namespace std;
const int N = 505;
const int dx[3][4] = {{-2, 0, 1, 0}, {-1, 0, 2, 0}, {-1, 0, 1, 0}},
          dy[3][4] = {{0, -2, 0, 1}, {0, -1, 0, 1}, {0, -1, 0, 2}},
          dl[3][4] = {{1, 2, 1, 2}, {0, 1, 0, 1}, {2, 0, 2, 0}};
struct State {
  int x, y, lie;
} q[N * N * 3];
int n, m;
int ex, ey;
char g[N][N];
int dist[N][N][3];
int hh, tt;

int main() {
  while (scanf("%d%d", &n, &m), n || m) {
    for (int i = 0; i < n; ++i) scanf("%s", g[i]);
    int x, y, lie;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        if (g[i][j] == 'X') {
          x = i, y = j, g[i][j] = '.';
          if (g[i + 1][j] == 'X') {
            lie = 1, g[i + 1][j] = '.';
          } else if (g[i][j + 1] == 'X') {
            lie = 2, g[i][j + 1] = '.';
          } else
            lie = 0;
        } else if (g[i][j] == 'O') {
          ex = i, ey = j, g[i][j] = '.';
        }
      }
    int res = -1;

    // [hh, tt] closed interval
    hh = tt = 0;
    q[0] = {x, y, lie};
    memset(dist, -1, sizeof(dist));
    dist[x][y][lie] = 0;

    while (hh <= tt) {
      auto [x, y, lie] = q[hh++];
      if (x == ex && y == ey && lie == 0) {
        res = dist[x][y][0];
        break;
      }

      for (int i = 0; i < 4; ++i) {
        int nx = x + dx[lie][i], ny = y + dy[lie][i], nlie = dl[lie][i];
        if (g[nx][ny] == '#') continue;
        if (nlie == 0 && g[nx][ny] == 'E') continue;
        if (nlie == 1 && g[nx + 1][ny] == '#') continue;
        if (nlie == 2 && g[nx][ny + 1] == '#') continue;
        if (dist[nx][ny][nlie] == -1) {
          dist[nx][ny][nlie] = dist[x][y][lie] + 1;
          q[++tt] = {nx, ny, nlie};
        }
      }
    }
    if (res == -1)
      puts("Impossible");
    else
      printf("%d\n", res);
  }
}
